/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { Form, Input, Button, DatePicker } from 'antd';
import type { ConnectState } from '@/models/connect';
import { connect } from 'umi';

import type { SmsSendSearchIndexType } from '../types/SmsSendSearch';
import moment from 'moment';
import { getRanString, SmsSendSearchColumns } from './utils';
import CommonResetButton from './_common/CommonResetButton';
import CommonColumns from './_common/CommonColumns';
const FormItem = Form.Item;

interface FormType {
  time: any;
  phone: string;
}

const SmsSendSearch = (props: SmsSendSearchIndexType) => {
  const { screenSize, dispatch, smsSendSearchList } = props;
  const [form] = Form.useForm();
  const onFinish = (values: FormType) => {
    const { phone, time } = values;
    dispatch({
      type: 'customerSearch/getSmsSendSearch',
      payload: { phone, date: moment(time).format('YYYY-MM-DD') },
    });
  };

  const onReset = () => {
    form.resetFields();
    dispatch({
      type: 'customerSearch/save',
      payload: { smsSendSearchList: [] },
    });
  };

  return (
    <>
      <Form
        form={form}
        onFinish={onFinish}
        layout={screenSize ? 'vertical' : 'inline'}
        style={{ marginBottom: 20 }}
      >
        <FormItem
          label={'手机号/运单号'}
          name='phone'
          rules={[{ required: true, message: '请输入手机号/运单号' }]}
        >
          <Input allowClear style={{ width: 200 }} placeholder={'请输入手机号/运单号'} />
        </FormItem>
        <FormItem label={'日期'} name='time' initialValue={moment()}>
          <DatePicker style={{ width: 200 }} disabledDate={(event) => event > moment()} />
        </FormItem>
        <FormItem>
          <Button type='primary' htmlType='submit' loading={false}>
            查询
          </Button>
          <CommonResetButton onReset={onReset} />
        </FormItem>
      </Form>
      {smsSendSearchList.map((item) => (
        <CommonColumns
          key={getRanString()}
          screenSize={screenSize}
          columns={SmsSendSearchColumns}
          data={item}
          isDoubleColumns
        />
      ))}
    </>
  );
};

export default connect(({ customerSearch, loading }: ConnectState) => ({
  smsSendSearchList: customerSearch.smsSendSearchList || [],
  tableLoading: loading.effects['customerSearch/getSmsSendSearch'] || false,
}))(React.memo(SmsSendSearch));
